
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class Animation1 extends StatefulWidget {
  const Animation1({Key? key}) : super(key: key);

  @override
  _Animation1State createState() => _Animation1State();
}

class _Animation1State extends State<Animation1>
    with SingleTickerProviderStateMixin {
  late AnimationController animationController;
  late CurvedAnimation curvedAnimation;
  late Animation<double> animate;
  late Animation<Color?> colorAnimate;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text("动画"),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: <Widget>[
              InkWell(
                onTap: () {
                  animationController.stop();
                },
                child: const Text("\n关闭动画\n"),
              ),
              InkWell(
                onTap: (){
                  animationController.forward();
                },
                child: const Text("开始动画\n"),
              ),
              AnimatedBuilder(
                  animation: animate,
                  builder: (context, child) => Container(
                        width: animate.value,
                        height: animate.value,
                        color: colorAnimate.value??Colors.yellow,
                        child: child,
                      ))
            ],
          ),
        ),
      );
  }

  @override
  void dispose() {
    super.dispose();
    animationController.dispose();
  }

  @override
  void initState() {
    super.initState();
    animationController =
        AnimationController(vsync: this, duration: const Duration(seconds: 2));
    curvedAnimation =
        CurvedAnimation(parent: animationController, curve: Curves.bounceInOut);
    animate = Tween(begin: 300.0, end: 100.0).animate(curvedAnimation);
    colorAnimate = ColorTween(begin: Colors.red,end: Colors.indigo).animate(curvedAnimation);
    animationController.addStatusListener((status) {
      switch(status){
        case AnimationStatus.dismissed:
          animationController.forward();
        break;
        case AnimationStatus.forward:
          break;
        case AnimationStatus.reverse:
          break;
        case AnimationStatus.completed:
          animationController.reverse();
          break;
      }
    });
    //启动动画
    // animationController.forward();
  }
}
